package com.nclg.exam.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.Random;

@Component
public class MailUtils {
    static Properties properties;
    static Message msg;
    static Transport transport;
    private static String subject;
    private static String sender;
    private static String user;
    private static String password;

    @Value("${email.subject}")
    public void setSubject(String subject) {
        this.subject = subject;
    }

    @Value("${email.sender}")
    public void setSender(String sender) {
        this.sender = sender;
    }

    @Value("${email.user}")
    public void setUser(String user) {
        this.user = user;
    }

    @Value("${email.password}")
    public void setPassword(String password) {
        this.password = password;
    }

    //初始化Mail信息
    public static void init(){
		/*// 网易163邮箱的 SMTP 服务器地址为: smtp.163.com    腾讯: smtp.qq.com
	    private  String myEmailSMTPServer = "smtp.qq.com";*/
        properties = new Properties();
        properties.setProperty("mail.debug", "true");//调试模式发送
        properties.setProperty("mail.smtp.auth", "true");//身份验证设置
        properties.setProperty("mail.host", "smtp.qq.com");//发件人邮箱主机名
        properties.setProperty("mail.transport.protocol", "smtp");//发件协议
        final String smtpPort = "465";
        properties.setProperty("mail.smtp.port", smtpPort);
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.setProperty("mail.smtp.socketFactory.fallback", "false");
        properties.setProperty("mail.smtp.socketFactory.port", smtpPort);
        properties.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance(properties);
        msg = new MimeMessage(session);
        try {
            msg.setSubject(subject);//邮箱主题
            msg.setFrom(new InternetAddress(sender));//设置发件人
            transport = session.getTransport();
            transport.connect(user, password);//设置发件人在该邮箱主机上的用户名密码
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 得到邮箱地址邮箱内容发送。
     * @param
     * @param
     * @throws AddressException
     * @throws MessagingException
     */
    public static void sendMail(String address,String text) throws AddressException, MessagingException {
        init();
        msg.setText(text);
        transport.sendMessage(msg, new Address[] {new InternetAddress(address)});
        transport.close();
    }

    /**
     * 随机生成6位数验证码
     * @return
     */
    public static String getCode(){
        String code = "";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            int r = random.nextInt(10); //每次随机出一个数字（0-9）
            code = code + r;  //把每次随机出的数字拼在一起
        }
        return code;
    }


}